# -*- coding: utf-8 -*-

__author__ = 'bilibili'

import types
import copy
from spot_excel_reader import spot_excel_reader
from spot_config import spot_config
from spot_excel_analyzer_base import spot_excel_analyzer_base

class spot_prod_data_analyzer(spot_excel_analyzer_base):
    # FUNCTION: __init__(self, string)
    #   the constructor of the class spot_prod_data_analyzer
    # ARGS:
    #   @config_name: json config file name
    def __init__(self, config_name):
        spot_excel_analyzer_base.__init__(self, config_name)
        self.theater_dict = {}
        self.area_dict = {}

    def fetch_key(self, in_dict):
        return in_dict['PC Level 4 - AGS/GCS']

    def new_value(self, in_dict):
        return []

    def build_new_index(self, the_dict, key, value_dict):
        if not the_dict.has_key(value_dict[key]):
            the_dict[value_dict[key]] = []
        the_dict[value_dict[key]].append(value_dict)

    def value_operation(self, key, in_dict):
        self.res_dict[key].append(in_dict)
        self.build_new_index(self.theater_dict, 'SO Hierarchy - L2 - Theatre', in_dict)
        self.build_new_index(self.area_dict, 'SO Hierarchy - L3 - Area', in_dict)

    def post_build_operation(self):
        pass

    # FUNCTION: get_theater_dict(self, string)
    #   the index from the theater
    # ARGS:
    #   @theater: the theater key
    # RETURN:
    #   dict
    def get_theater_dict(self, theater):
        return self.theater_dict[theater]

    # FUNCTION: get_area_dict(self, string)
    #   the index from the theater
    # ARGS:
    #   @area: the area key
    # RETURN:
    #   dict
    def get_area_dict(self, area):
        return self.area_dict[area]

    def get_level_4_dict(self, in_dict):
        tmp_dict = {}
        for i_case in in_dict:
            if not tmp_dict.has_key(i_case['PC Level 4 - AGS/GCS']):
                tmp_dict[i_case['PC Level 4 - AGS/GCS']] = []
            tmp_dict[i_case['PC Level 4 - AGS/GCS']].append(i_case)
        return tmp_dict

    def get_theater_level_4_dict(self, theater):
        return self.get_level_4_dict(self.get_theater_dict(theater))

    def get_area_level_4_dict(self, area):
        return self.get_level_4_dict(self.get_area_dict(area))

    # FUNCTION: customized_calculation(self, [year, quarter])
    #   statistics of the ProdData data
    # ARGS:
    #   @list:
    #       year: the year key to be fetched
    #       quarter: the quarter key to be fetched
    # RETURN:
    #   dict: statistics of the ProdData data
    def customized_calculation(self, arg_list):
        year = arg_list[0]
        quarter = arg_list[1]
        res = {}
        current_column = year + quarter
        year = int(year)
        quarter = int(quarter)
        quarter -= 1
        if quarter <= 0:
            quarter += 4
            year -= 1
        preview_column = str(year) + str(quarter)
        if len(arg_list) <= 2 or arg_list[2] == '' or arg_list[3] == '':
            analyzer_dict = self.get_result_dict()
            res['category'] = ''
            res['name'] = 'Global'
        else:
            index_key = arg_list[2]
            res['category'] = arg_list[2]
            res['name'] = arg_list[3]
            if index_key == 'theater':
                analyzer_dict = self.get_theater_level_4_dict(arg_list[3])
            elif index_key == 'area':
                analyzer_dict = self.get_area_level_4_dict(arg_list[3])
            else:
                raise Exception('Not Supported index_key')
        for (d, x) in analyzer_dict.items():
            key = d
            if key == '':
                continue
            current_qtr_money = 0
            preview_qtr_money = 0
            for i in x:
                if i[current_column]['Amount - USD'] == '' or i[preview_column]['Amount - USD'] == '':
                    continue
                current_qtr_money += float(i[current_column]['Amount - USD'])
                preview_qtr_money += float(i[preview_column]['Amount - USD'])
            tmp = {}
            tmp['current_qtr_money'] = current_qtr_money
            tmp['preview_qtr_money'] = preview_qtr_money
            res[key] = tmp
        return res

def test_spot_prod_data_analyzer():
    analyzer = spot_prod_data_analyzer('C:\\svn_repo\\SPOT\\trunk\\DashboardDataService\\resources\\ProdData.config.json')
    # print analyzer.build_entry(14, 0)
    analyzer.build_whole_table()
    for i in analyzer.get_area_dict('Brazil'):
        print i

if __name__ == '__main__':
    test_spot_prod_data_analyzer()